use strict;
use warnings;

package Lingua::EN::WSD::WebBased::Expander::GoogleWithFullDocumentContent;

use base qw/Lingua::EN::WSD::WebBased::Expander/;

use Lingua::EN::WSD::WebBased::Utils;
use Lingua::EN::WSD::WebBased::Logger;

use File::Spec;
use File::Basename;
use JSON::XS;

use Class::AccessorMaker {
    search_cmd_path     => [qw/.. .. .. .. .. .. bin search/],
    search_cmd          => undef,
    snippet_only        => 1
}, 'new_init';

sub init {

    my $self = shift;

    $self->SUPER::init;

    $self->{search_cmd} = 
        File::Spec->catfile(dirname(__FILE__), @{$self->search_cmd_path});

}

sub search {

    my ($self, $query, $size) = @_;

    _trace(__PACKAGE__, "::search($query, $size)");

    $query =~ s/'/ /g;

    my $cmd = $self->search_cmd . " -q '$query'";
    $cmd .= ' -s' if $self->snippet_only;

    _trace("$cmd");

    my $documents = decode_json(qx/$cmd/)->{results};
    $documents = [grep { ($_ || '') =~ /./ } @$documents];

    return $documents;

}

1;
